package com.example.administrator.login;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.BinderThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class LoginActivity extends AppCompatActivity {

    private EditText accountText, keyText;
    private Button forgetKeyButton, loginButton, inButton;

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        accountText = (EditText) findViewById(R.id.account);
        keyText = (EditText) findViewById(R.id.keying);
        forgetKeyButton = (Button) findViewById(R.id.forgetKey);
        loginButton = (Button) findViewById(R.id.login);
        inButton = (Button) findViewById(R.id.In);

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int a = msg.what;
                if (a == 1){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        };

        forgetKeyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, ForgetActivity.class);
                startActivity(intent);

            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singUp();
                Intent intent = new Intent(LoginActivity.this, PersonalActivity.class);
                startActivity(intent);
            }
        });

        inButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, SingInActivity.class);
                startActivity(intent);
            }
        });
    }

    //登录
    private void singUp(){
        new Thread(){
            @Override
            public void run() {
                super.run();

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://api.zhaibao.cn/login?mobile="+accountText.getText()+
                                                "&password="+keyText.getText()+"&version=1.0&deviceType=2");

              try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    if (httpResponse.getStatusLine().getStatusCode() == 200){
                        HttpEntity httpEntity = httpResponse.getEntity();

                        String str = EntityUtils.toString(httpEntity);

                        try {
                            JSONObject jsonObject = new JSONObject(str);
                            String success = jsonObject.getString("success");
                            JSONObject jsonObject1 = new JSONObject(success);
                            String mgs = jsonObject1.getString("message");
                            int code = mgs.indexOf("code");

                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
